package com.py.custom.common.web.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * description： spring容器 获取 bean的工具类
 *
 * @author pengyou
 * @version 1.0.0
 * @date 2021/10/23
 */
@Component
public class ApplicationContextUtils {

    @Autowired
    private ApplicationContext applicationContext;

    private static ApplicationContext staticApplicationContext;

    @PostConstruct
    public void init() {
        staticApplicationContext = applicationContext;
    }

    public static <T> T getBean(Class<T> clazz) {
        if (staticApplicationContext != null) {
            return staticApplicationContext.getBean(clazz);
        }
        return null;
    }
}
